package de.beuth.hochschule.ats.activitys;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

import de.beuth.hochschule.ats.R;
import de.beuth.hochschule.ats.entitys.DatabaseHelper;
import de.beuth.hochschule.ats.entitys.Lecture;
import de.beuth.hochschule.ats.entitys.Vocable;
import de.beuth.hochschule.ats.util.LanguageMap;

public class EditVocable extends OrmLiteBaseActivity<DatabaseHelper> implements
		OnClickListener {
	private String action;
	private TextView questionLabel, answerLabel;
	private AutoCompleteTextView questionInput, answerInput;
	private Lecture lecture;
	private Vocable vocable;
	private AlertDialog.Builder builder;
	private AlertDialog dialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("Info", "Card View wurde geladen!");

		setContentView(R.layout.edit_vocable);

		action = getIntent().getAction();

		findViewById(R.id.save_btn).setOnClickListener(this);
		findViewById(R.id.cancel_btn).setOnClickListener(this);
		questionLabel = (TextView) findViewById(R.id.question_label);
		answerLabel = (TextView) findViewById(R.id.answer_label);
		questionInput = (AutoCompleteTextView) findViewById(R.id.question_input);
		answerInput = (AutoCompleteTextView) findViewById(R.id.answer_input);

		int lecture_id, vocable_id;
		if (action.equals(Intent.ACTION_EDIT)) {
			setTitle(R.string.edit_vocable);
			if (savedInstanceState == null) {
				vocable_id = getIntent().getExtras().getInt("vocable_id");
			} else {
				vocable_id = savedInstanceState.getInt("vocable_id");
			}
			try {
				vocable = getHelper().getVocableDao().queryForId(vocable_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lecture = vocable.getLecture();
			fillInputFields();
		} else {
			setTitle(R.string.new_vocable);
			if (savedInstanceState == null) {
				lecture_id = getIntent().getExtras().getInt("lecture_id");
			} else {
				lecture_id = savedInstanceState.getInt("lecture_id");
			}
			try {
				lecture = getHelper().getLectureDao().queryForId(lecture_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		LanguageMap languageMap = new LanguageMap();
		if (languageMap.containsValue(lecture.getNativeLanguage())) {
			questionLabel.setText(new Locale(lecture.getNativeLanguage()).getDisplayLanguage());
		} else {
			questionLabel.setText(R.string.other_language);
		}
		if (languageMap.containsValue(lecture.getForeignLanguage())) {
			answerLabel.setText(new Locale(lecture.getForeignLanguage()).getDisplayLanguage());
		} else {
			answerLabel.setText(R.string.other_language);
		}

		buildSaveCardDialog();
	}

	public void onClick(View v) {

		String question = questionInput.getText().toString().trim();
		String answer = answerInput.getText().toString().trim();

		switch (v.getId()) {

		case R.id.save_btn:
			if (action.equals(Intent.ACTION_EDIT)) {
				try {
					vocable.update(question, answer, lecture);
					getHelper().getVocableDao().update(vocable);
					finish();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					vocable = new Vocable();
					vocable.update(question, answer, lecture);
					getHelper().getVocableDao().create(vocable);
					dialog.show();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;

		case R.id.cancel_btn:
			finish();

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate menu from XML resource
		if (action.equals(Intent.ACTION_EDIT)) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.vocable_menu, menu);			
		}


		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.delete:
			Dao<Vocable, Integer> vocableDao;
			try {
				vocableDao = getHelper().getVocableDao();
				vocableDao.delete(vocable);
				finish();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void fillInputFields() {
		questionInput.setText(vocable.getQuestion());
		answerInput.setText(vocable.getAnswer());
	}

	private void buildSaveCardDialog() {
		builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.save_card_dialog_succes)
				.setCancelable(false)
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								questionInput.setText("");
								answerInput.setText("");
								questionInput.requestFocus();
								dialog.cancel();
							}
						})
				.setNegativeButton(R.string.no,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								EditVocable.this.finish();
							}
						});
		dialog = builder.create();
	}

}
